package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Inventory;
import com.ruoyi.system.domain.MaterialSix;
import com.ruoyi.system.domain.MaterialTwo;
import com.ruoyi.system.domain.PickCondition;
import com.ruoyi.system.service.IWarehouseDetailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/WarehouseMateriaDetail")//仓库材料明细
public class WarehouseDetailController extends BaseController
{

    @Autowired
    private IWarehouseDetailService warehouseService;

    /**
     * 查询仓库材料的明细  要知道明细记录在那个表中
     */
    @PostMapping("/MateriaDetaillList")
    public TableDataInfo MateriaDetaillList(PickCondition pickCondition, Integer mid, Integer wid)
    {
        startPage();
        List<MaterialSix> list =warehouseService.selectMaterialList(pickCondition,wid,mid);
        return getDataTable(list);
    }

    /**
     * 查询仓库材料的明细  要知道明细记录在那个表中
     */
    @PostMapping("/MaterialRecordList")
    public TableDataInfo MaterialRecordList(PickCondition pickCondition, Integer mid, Integer wid)
    {
        startPage();
        List<MaterialTwo> list =warehouseService.selectMaterialRecordList(pickCondition,wid,mid);
        return getDataTable(list);
    }

    @Log(title = "仓库", businessType = BusinessType.DELETE)
    @DeleteMapping( "/remove/{mdid}")
    public AjaxResult deleteMateriaDetaill(@PathVariable Long mdid){
        if(warehouseService.deleteMateriaDetaill(mdid)>0){
            return success();
        }else {
            return error();
        }

    }
    //盘点接口
    @PostMapping("/inventory")
    public AjaxResult inventory(@RequestBody(required = false) Inventory inventory) throws ParseException {
        Calendar instance = Calendar.getInstance();
        int month=instance.get(Calendar.MONTH)+1;
        Date parse = new SimpleDateFormat("yyyy-MM").parse(instance.get(Calendar.YEAR) + "-" +month);
        inventory.setDate(parse);
        warehouseService.updateInventory(inventory);
        return success("已保存，请尽快去仓库处理");
    }
    @PostMapping("/update")
    public AjaxResult updateWarehouseMateriaDetail(MaterialSix materialSix){
        int a=warehouseService.updateWarehouseMateriaDetail(materialSix);
        if(a>0)return AjaxResult.success();else return AjaxResult.error();
    }
    @GetMapping("/Material")
    public AjaxResult queryMaterial(@Param("type") int type,
                                    @Param("mName")String mName,
                                    @Param("mModel")String mModel,
                                    @Param("mUnit")String mUnit,
                                    @Param("proName")String proName,
                                    @Param("proNumber")String proNumber,

                                    @Param("wName")String wName){
        System.out.println(proName);
        System.out.println(proNumber);
        return AjaxResult.success(warehouseService.queryMaterial(type,mModel,mName,mUnit,proName,proNumber,wName));
    }
    /**
     * 导出项目列表
     */
    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/MaterialRecordList/export")
    public AjaxResult exportMaterialRecordList(PickCondition pickCondition, Integer mid, Integer wid)
    {
        List<MaterialTwo> list = warehouseService.selectMaterialRecordList(pickCondition,wid,mid);
        ExcelUtil<MaterialTwo> util = new ExcelUtil<MaterialTwo>(MaterialTwo.class);
        return util.exportExcel(list, "MaterialTwo");
    }


    @Log(title = "项目", businessType = BusinessType.EXPORT)
    @PostMapping("/MateriaDetaillList/exports")
    public AjaxResult exportMateriaDetaillList(PickCondition pickCondition, Integer mid, Integer wid)
    {
        List<MaterialSix> list = warehouseService.selectMaterialList(pickCondition,wid,mid);
        ExcelUtil<MaterialSix> util = new ExcelUtil<MaterialSix>(MaterialSix.class);
        return util.exportExcel(list, "MaterialSix");
    }
}
